<?php
/**
 * @developer socheat
 * @copyright 2012
 */
require_once('../libraryfiles/config.php');

$action = isset($_GET['action']) ? $_GET['action'] : '';

switch($action)
{
	case 'add' :
		add();
		break;

	case 'update' :
		update();
		break;


		
	default :
	    // if action is not defined or unknown
		// move to main user page
		header('Location: index.php');
		exit();
}
function update()
    {
        if(!empty($_POST['info']))
        {
            echo $_POST['info'];
            $theme          = mysql_query("UPDATE tbl_style AS theme SET theme.`sitename`='".$_POST['sitename']."', theme.`site_tagline`='".$_POST['tagline']."', theme.`timezone`='".$_POST['timezone']."', theme.`language`='".$_POST['language']."'  WHERE theme.`by_company_id`='".$_SESSION['acc_id']."'");
            $descript       = mysql_query("UPDATE tblpharmacy AS com SET com.`descriptions`='".$_POST['tagline']."' WHERE com.`user_id`='".$_SESSION['acc_id']."'");
            if($theme)
                        {
                            header('Location: dashboard/index.php?message=Your setting has been change!&class=success');
                        }
                        else
                        {
                            header('Location: dashboard/index.php?message=Your setting can not be change!&class=error');
                        }
        }
         
        if(!empty($_POST['sorturls']))
        {
            $wseller            = "com.`shorturl`='".$_POST['sorturl']."'"; 
            $getseller          = new seller();
            $seller_all         = $getseller->sellers($wseller); 
            if(dbNumRows($seller_all))
                {
                    $error      = 'Sorry, This url have already register!';
    				header("Location: dashboard/index.php?message=$error&class=error");
                }
                else
                {
                    $sortusl    = mysql_query("UPDATE tblpharmacy AS com SET com.`shorturl`='".$_POST['sorturl']."' WHERE com.`user_id`='".$_SESSION['acc_id']."'");
                    header('Location: dashboard/index.php?message=Your setting has been change!&class=success');                    
                } 
        }
        
        
        if(!empty($_POST['chlogo']))
        {
            function uploadPhoto($inputName, $uploadDir)
                {
                	 $image     = $_FILES[$inputName];
                	 $imagePath = '';
                	 $thumbnailPath = '';
                	 
                	 // if a file is given
                	 if (trim($image['tmp_name']) != '') {
                	  $ext = substr(strrchr($image['name'], "."), 1); //$extensions[$image['type']];
                	
                	  // generate a random new file name to avoid name conflict
                	  $imagePath = $image['name'];
                	  
                	  list($width, $height, $type, $attr) = getimagesize($image['tmp_name']); 
                	  if ( true && $width > 500) {
                	   $result    = createThumbnail($image['tmp_name'], $uploadDir . $imagePath, 500);
                	   $imagePath = $result;
                	  } else {
                	   $result = move_uploaded_file($image['tmp_name'], $uploadDir . $imagePath);
                	  } 
                	 
                	  if ($result) {
                	   // create thumbnail
                	   $thumbnailPath  =  md5(rand() * time()) . ".$ext";
                	   $size         = getimagesize($uploadDir . $imagePath);
                	   if($size[0]>100)
                		$result   = createThumbnail($uploadDir . $imagePath, $uploadDir . $thumbnailPath, 100);
                	   else{
                		copy($uploadDir . $imagePath, $uploadDir . $thumbnailPath);
                		$result   = basename($uploadDir . $thumbnailPath);
                	   }
                		
                	   // create thumbnail failed, delete the image
                	   if (!$result) {
                		unlink($uploadDir . $imagePath);
                		$imagePath = $thumbnailPath = '';
                	   } else {
                		$thumbnailPath = $result;
                	   } 
                	  } else {
                	   // the product cannot be upload / resized
                	   $imagePath = $thumbnailPath = '';
                	  }
                	  
                	 }
                	 return array('image' => $imagePath, 'thumbnail' => $thumbnailPath); 
                }
            // end uploadPhoto
            function createThumbnail($srcFile, $destFile, $width, $quality = 75)
                {
                	 $thumbnail = '';
                	 
                	 if (file_exists($srcFile)  && isset($destFile))
                	 {
                	  $size        = getimagesize($srcFile);
                	  $w           = number_format($width, 0, ',', '');
                	  $h           = number_format(($size[1] / $size[0]) * $width, 0, ',', '');
                	  
                	  $thumbnail =  copyImage($srcFile, $destFile, $w, $h, $quality);
                	 }
                	 
                	 // return the thumbnail file name on sucess or blank on fail
                	 return basename($thumbnail);
                }
             //end createThumbnail   
            
            function copyImage($srcFile, $destFile, $w, $h, $quality = 75)
                {
                		$tmpSrc     = pathinfo(strtolower($srcFile));
                		$tmpDest    = pathinfo(strtolower($destFile));
                		$size       = getimagesize($srcFile);
                	
                		if ($tmpDest['extension'] == "gif" || $tmpDest['extension'] == "jpg")
                		{
                		   $destFile  = substr_replace($destFile, 'jpg', -3);
                		   $dest      = imagecreatetruecolor($w, $h);
                		   imageantialias($dest, TRUE);
                		} elseif ($tmpDest['extension'] == "png") {
                		   $dest = imagecreatetruecolor($w, $h);
                		   imageantialias($dest, TRUE);
                		} else {
                		  return false;
                		}
                	
                		switch($size[2])
                		{
                		   case 1:       //GIF
                			   $src = imagecreatefromgif($srcFile);
                			   break;
                		   case 2:       //JPEG
                			   $src = imagecreatefromjpeg($srcFile);
                			   break;
                		   case 3:       //PNG
                			   $src = imagecreatefrompng($srcFile);
                			   break;
                		   default:
                			   return false;
                			   break;
                		}
                	
                		imagecopyresampled($dest, $src, 0, 0, 0, 0, $w, $h, $size[0], $size[1]);
                	
                		switch($size[2])
                		{
                		   case 1:
                		   case 2:
                			   imagejpeg($dest,$destFile, $quality);
                			   break;
                		   case 3:
                			   imagepng($dest,$destFile);
                		}
                		return $destFile;
                	
                }
            //end copyImage
            
                
                
       	    $image = uploadPhoto('logo_image','uploads/');
            $logo = $image['thumbnail']; 
            	$logoch = "UPDATE tblpharmacy SET 
				pharmacy_image='$logo' WHERE tblpharmacy.user_id=".$_SESSION['acc_id'];	
            	mysql_query($logoch);
            	
            	$success = 'You have successfully edit your logo!';
            	header("Location: ".W_ROOT."/company/action/?p=setting/index&success=$success");                
        }
        //end change logo
        
    }
    // end update
?>